﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;
using T.ServiceComponent.DataAccessEx.Core;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataQueryColumnELBDao : DaoService<DataQueryColumn>
    {
        public override string TableName => "FAS_DataQueryColumn";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataQueryID";
        public override List<string> ExceptColumnName {get;} = new List<string> { "DataObject", "DataColumn" };

        public DataQueryColumnELBDao() : base()
        {
            DBName = "TFAS";
        }

        public override DataQueryColumn ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            if (columns.ContainsKey("ColumnType"))
                columns.Remove("ColumnType");

            if (columns.ContainsKey("AggregateType"))
                columns.Remove("AggregateType");

            var  data = base.ConvertToObject(columns, row);

            data.ColumnType = (ColumnType)Enum.Parse(typeof(ColumnType), Convert.ToString(row["ColumnType"]));
            data.AggregateType = (AggregateType)Enum.Parse(typeof(AggregateType), Convert.ToString(row["AggregateType"]));

            return data;
        }

        public override Dictionary<string, object> GetDataMapping(DataQueryColumn data)
        {
            var mapping = base.GetDataMapping(data);
            mapping.Remove("ColumnType");
            mapping.Remove("AggregateType");
            mapping.Remove("DataObject");
            mapping.Remove("DataColumn");

            mapping.Add("ColumnType", (int)data.ColumnType);
            mapping.Add("AggregateType", (int)data.AggregateType);

            return mapping;
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DataQueryColumn> GetDataQueryColumns(string dqId)
        {
            var result = new List<DataQueryColumn>();
            if (string.IsNullOrEmpty(dqId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{dqId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataQueryColumn));
            }
            return result;
        }

        /// <summary>
        /// 添加DataQueryColumn
        /// </summary>
        /// <param name="dqColumns"></param>
        public void CreateDataQueryColumn(List<DataQueryColumn> dqColumns)
        {
            Add(new List<object>(dqColumns));       
        }

        /// <summary>
        /// 删除DataQueryColumn
        /// </summary>
        /// <param name="dqId"></param>
        public void DeleteDataQueryColumn(string dqId)
        {
            var parameter = DB.MakeInParam("@DataQueryID", TDbDataType.VarChar, dqId);

            DB.ExecSqlStatement(string.Format("DELETE FROM {0} WHERE DataQueryID=@DataQueryID", this.TableName), parameter);
        }

        /// <summary>
        /// 删除DataQueryColumn
        /// </summary>
        /// <param name="dqIds"></param>
        public void BatchDeleteDataQueryColumn(List<string> dqIds)
        {
            try
            {
                List<string> sqls = new List<string>();
                foreach (string dqId in dqIds)
                {
                    sqls.Add(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
                }
                base.DB.ExecSqlStatement(sqls.ToArray());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteError, e.Message, e);
            }
        }
    }
}
